package com.catmiw.csdn;

import java.io.*;
import java.util.List;

import org.jdom2.*;
import org.jdom2.output.*;
import org.jdom2.output.Format.TextMode;
import org.jdom2.input.*;

public class JdomDemo {

	public static void main(String[] args) 
	{
		jdomWriter();//1.先写入
		
		try {
			jdomReader();//2.后读取
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void jdomWriter()
	{
		//一：顶级节点
		Element topTree = new Element("topTree");
		Document doc = new Document(topTree);//将顶级节点（唯一一个）设置进Document
        Comment ttComment = new Comment("测试将数据从程序输出到XML中");//创建注释
        //ttComment.
        topTree.addContent(ttComment);
        
		//二：子节点1
		Element subTree1 = new Element("subTree");
		Attribute ab1 = new Attribute("ab","attributeOfsubTree1");//子节点属性
		subTree1.setAttribute(ab1);
		
		Element object1 = new Element("object1");//子节点信息
		Element object2 = new Element("object2");
		Element object3 = new Element("object3");
		object1.setText("valueOfObj1");
		object2.setText("valueOfObj2");
		object3.setText("valueOfObj3");
		
		topTree.addContent(subTree1);//与上级节点（顶级节点）关系
		subTree1.addContent(object1);
		subTree1.addContent(object2);
		subTree1.addContent(object3);
		
		
		
		//三：子节点2
		Element subTree2 = new Element("subTree");
		Attribute ab2 = new Attribute("ab","attributeOfsubTree2");//子节点属性
		subTree2.setAttribute(ab2);
		
		Element object4 = new Element("object1");//子节点信息
		Element object5 = new Element("object2");
		Element object6 = new Element("object3");
		object4.setText("valueOfObj4");//!!!注意不能再用object1对象，否则会出现不能加入上级节点的错误
		object5.setText("valueOfObj5");
		object6.setText("valueOfObj6");
		
		topTree.addContent(subTree2);//与上级节点（顶级节点）关系
		subTree2.addContent(object4);
		subTree2.addContent(object5);
		subTree2.addContent(object6);
		
			
		
		//四：将上述内容写入XML
		XMLOutputter out = new XMLOutputter();
		Format format=out.getFormat();
		format.setIndent("\t");
		format.setTextMode(TextMode.TRIM );
		//format.
		//format.set
		format.setEncoding("UTF-8");
		//out.setFormat(out.getFormat().setEncoding("GBK"));
		try {
			out.output(doc, new FileOutputStream(new File("e:/jdom_demo1.xml")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	public static void jdomReader() throws Exception
	{
		SAXBuilder builder = new SAXBuilder();
		Document read_doc = builder.build(new File("e:/jdom_demo1.xml"));
		Element root = read_doc.getRootElement(); //取得根节点
		List<?> list = root.getChildren("subTree");
		
		for(int i=0;i<list.size();i++)
		{
			Element e = (Element)list.get(i);//取得子节点
			String obj1 = e.getChildText("object1");
			String obj2 = e.getChildText("object2");
			String obj3 = e.getChildText("object3");
			
			System.out.println("No."+i);
			System.out.println("object1=> "+obj1);
			System.out.println("object2=> "+obj2);
			System.out.println("object3=> "+obj3);
			System.out.println("---------------");
		}
		
			
	}
}
